<template>
  <div class="video-player-container">
    <div class="header-controls">
      <button @click="handleBack" class="back-btn">← 返回</button>
      <h2 class="movie-title">{{ movieTitle }}</h2>
    </div>
    <video 
      ref="videoRef"
      :src="videoSource"
      @timeupdate="updateProgress"
      @loadedmetadata="updateProgress"
      @play="isPlaying = true"
      @pause="isPlaying = false"
      @fullscreenchange="handleFullscreenChange"
      class="video-player"
    ></video>
    
    <div class="controls">
      <button @click="togglePlay" class="play-btn">
        {{ isPlaying ? '暂停' : '播放' }}
      </button>
      
      <div class="progress-container">
        <input 
          type="range" 
          min="0" 
          max="100" 
          :value="(currentTime / duration) * 100 || 0" 
          @input="setProgress"
          class="progress-bar"
        >
        <div class="time-display">
          <span>{{ formatTime(currentTime) }}</span> / <span>{{ formatTime(duration) }}</span>
        </div>
      </div>
      
      <div class="volume-control">
        <input 
          type="range" 
          min="0" 
          max="1" 
          step="0.1" 
          v-model="volume"
          @input="setVolume"
          class="volume-slider"
        >
      </div>
      
      <button @click="toggleFullScreen" class="fullscreen-btn">
        {{ isFullScreen ? '退出全屏' : '全屏' }}
      </button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { movieService } from '@/services/movieService';
import type { MovieDetails } from '@/dataType/movieType';

const route = useRoute();
const router = useRouter();
const episodeId = route.params.id as string;
const videoSource = ref<string>(`http://localhost:8080/file/download/190929aa-4915-449b-9a39-22ad65ad1cca`);
const isPlaying = ref<boolean>(false);
const currentTime = ref<number>(0);
const duration = ref<number>(0);
const volume = ref<number>(1);
const videoRef = ref<HTMLVideoElement | null>(null);
const movieTitle = ref<string>('');
const isFullScreen = ref<boolean>(false);

const handleBack = () => {
  router.go(-1);
};

const togglePlay = () => {
  if (videoRef.value) {
    if (isPlaying.value) {
      videoRef.value.pause();
    } else {
      videoRef.value.play();
    }
    isPlaying.value = !isPlaying.value;
  }
};

const updateProgress = () => {
  if (videoRef.value) {
    currentTime.value = videoRef.value.currentTime;
    duration.value = videoRef.value.duration || 0;
  }
};

const setProgress = (e: Event) => {
  if (videoRef.value && duration.value) {
    const target = e.target as HTMLInputElement;
    const percent = parseFloat(target.value);
    videoRef.value.currentTime = (percent / 100) * duration.value;
  }
};

const setVolume = (e: Event) => {
  if (videoRef.value) {
    const target = e.target as HTMLInputElement;
    volume.value = parseFloat(target.value);
    videoRef.value.volume = volume.value;
  }
};

const formatTime = (timeInSeconds: number): string => {
  const minutes = Math.floor(timeInSeconds / 60);
  const seconds = Math.floor(timeInSeconds % 60);
  return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
};

const toggleFullScreen = () => {
  if (!videoRef.value) return;

  if (!document.fullscreenElement) {
    videoRef.value.requestFullscreen().catch(err => {
      console.error(`Error attempting to enable full-screen mode: ${err.message}`);
    });
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
  }
};

const handleFullscreenChange = () => {
  isFullScreen.value = !!document.fullscreenElement;
};

onMounted(() => {
  // 获取电影详情以显示标题
  movieTitle.value="影视标题"
  movieService.getMovieEpisodeById(Number(episodeId)).then(data => {
    movieTitle.value = data.title;
    videoSource.value = data.playUrl;
  });
});
</script>
<style scoped>
.video-player-container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 15px;
  position: relative;
}

.back-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: var(--color-background);
  color: var(--color-text);
  cursor: pointer;
  z-index: 10;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background-color: rgba(30, 136, 229, 0.9);
}

.video-player {
  width: 100%;
  height: auto;
  border-radius: 8px;
  background-color: #000;
}

.controls {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 0 0 8px 8px;
}

.play-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #1E88E5;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s;
}

.play-btn:hover {
  background-color: #1565C0;
}

.progress-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.progress-bar {
  width: 100%;
  height: 6px;
  accent-color: #1E88E5;
}

.time-display {
  font-size: 0.9rem;
  color: #666;
  display: flex;
  justify-content: space-between;
}

.volume-control {
  width: 100px;
}

.volume-slider {
  width: 100%;
  accent-color: #1E88E5;
}

.fullscreen-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #1E88E5;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s;
}

.fullscreen-btn:hover {
  background-color: #1565C0;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 15px;
}

.movie-title {
  color: #333;
  font-size: 1.2rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 768px) {
  .movie-title {
    font-size: 1rem;
    max-width: 200px;
  }
  .video-player-container {
    padding: 10px;
  }

  .controls {
    flex-wrap: wrap;
    gap: 10px;
    padding: 8px;
  }

  .play-btn, .fullscreen-btn {
    padding: 6px 12px;
    font-size: 0.9rem;
  }

  .volume-control {
    width: 80px;
  }

  .time-display {
    font-size: 0.8rem;
  }
}
</style>